function [p,w, share, carrier_profit, oem_profit, cons_surplus, ...
    cons_surplus_no_error, expmu, prod_profit, elas] = equi_pwq(...
    para_nonlinear, randomdraw0, ...
    p0, w0,...
    x_rand, meanutility, ind_endogenous, carrier_ind, oem_ind, ...
    sim_pcoeff, rand_income, income_norm, mc, setup, x_rand2, same_firm)
% purpose: depending on "mode", compute either the oem pricing eqm, or the carrier pricing eqm
% inputs:
%   p0, w0, q0: either starting point (if it is endogenous) or the given wholesale price (w0), quality (q0) if it is exogenous
%   meanutility: part of the meanutility that is independent of quality and price

% check the number of simulation draws

para_nonlinear = reshape(para_nonlinear, [length(para_nonlinear), 1]);

[n_obs, n_x_rand] = size(x_rand);
randomdraw = gen_randomdraw(para_nonlinear,...
    n_x_rand, randomdraw0, false);


n_x_rand2 = size(x_rand2, 2);

mu = [x_rand, ones(n_obs, 1)] * randomdraw;

mu = mu + para_nonlinear(n_x_rand+4)*rand_income/income_norm ...
    + (x_rand2*para_nonlinear(n_x_rand+5:n_x_rand+n_x_rand2+4, :)).*rand_income/income_norm;


n_smln = size(mu, 2);
n_prod_t = size(p0, 1);

prod_profit = [];
elas = [];
switch setup.mode
    case 1
        % sequential models        
        exp_u_wo_price = exp(bsxfun(@plus, mu, meanutility));        
        
        [p, w, share, mc, carrier_profit, oem_variableprofit] = ...
            equi_pw0(p0, w0, exp_u_wo_price, mc, ind_endogenous, ...
            carrier_ind, oem_ind, sim_pcoeff, setup);
        
        oem_profit = oem_variableprofit;
        
        prod_profit = (p-mc).*share;        
    case 2
        % direct_oem, direct_carrier or oem_carrier_collusion        
        exp_u_wo_price = exp(bsxfun(@plus, mu, meanutility));
        
        if isequal(setup.pricing_model, 'direct_oem') || isequal(setup.pricing_model, 'captain')            
            [p, share, ~, ~, ~, ~, elas] = equi_p0(p0, mc, exp_u_wo_price, ind_endogenous,...
                oem_ind, sim_pcoeff, same_firm, setup);
        elseif isequal(setup.pricing_model, 'sequential') 
            
            [p, share] = equi_pw0(p0, w0, exp_u_wo_price, mc,...
                ind_endogenous, carrier_ind, oem_ind, sim_pcoeff, setup);
            
        elseif isequal(setup.pricing_model, 'direct_carrier') 
            
            [p, share] = equi_p0(p0, mc, exp_u_wo_price, ind_endogenous,...
                carrier_ind, sim_pcoeff, setup);
            
        elseif isequal(setup.pricing_model, 'oem_carrier_collusion')
            collusionweightmatrix = zeros(n_prod_t, n_prod_t);
            for no = 1 : size(oem_ind,2)
                Jot = oem_ind(:, no);
                for nc = 1 : size(carrier_ind,2)
                    Jct = carrier_ind(:, nc);
                    collusionweightmatrix(Jot&Jct, Jot&Jct) = 1;
                    collusionweightmatrix(Jot&Jct, Jot&~Jct) = setup.collusion_para_oem * setup.collusion_para_oem_share_of_profit; 
                    collusionweightmatrix(Jot&Jct, ~Jot&Jct) = setup.collusion_para_carrier * (1-setup.collusion_para_oem_share_of_profit);
                end
            end
            setup.collusionweightmatrix = collusionweightmatrix;
            [p, share] = equi_p0(p0, mc, exp_u_wo_price, ind_endogenous,...
                carrier_ind, sim_pcoeff, setup);
        end
        
        prod_profit = (p-mc).*share;
        
        if isequal(setup.pricing_model, 'direct_oem')
            carrier_profit = zeros(size(carrier_ind, 2), 1);
        
            oem_variableprofit = zeros(size(oem_ind,2), 1);
            for no = 1 : size(oem_ind,2)
                if any(oem_ind(:,no))
                    oem_variableprofit(no) = sum(prod_profit(oem_ind(:, no)));
                end
            end
        elseif isequal(setup.pricing_model, 'direct_carrier')
            oem_variableprofit = zeros(size(oem_ind,2), 1);
            
            carrier_profit = zeros(size(carrier_ind, 2), 1);
            for nc = 1 : size(carrier_ind,2)
                if any(carrier_ind(:,nc))
                    carrier_profit(nc) = sum(prod_profit(carrier_ind(:, nc)));
                end
            end
        elseif isequal(setup.pricing_model, 'oem_carrier_collusion')
            oem_variableprofit = zeros(size(oem_ind,2), 1);
            for no = 1 : size(oem_ind,2)
                if any(oem_ind(:,no))
                    oem_variableprofit(no) = sum(prod_profit(oem_ind(:, no)));
                end
            end
            oem_variableprofit = setup.collusion_para_oem_share_of_profit * oem_variableprofit;
            
            carrier_profit = zeros(size(carrier_ind, 2), 1);
            for nc = 1 : size(carrier_ind,2)
                if any(carrier_ind(:,nc))
                    carrier_profit(nc) = sum(prod_profit(carrier_ind(:, nc)));
                end
            end
            carrier_profit = (1-setup.collusion_para_oem_share_of_profit) * carrier_profit;
            
        end
        
        
        
        oem_profit = oem_variableprofit;
        
        w = nan(size(p));
end

if nargout > 6
    
    u_wo_price = log(exp_u_wo_price);
    
    mu = bsxfun(@plus, u_wo_price, p.*sim_pcoeff);
    cons_surplus_no_error = mean(max([mu; zeros(1,n_smln)], [], 1)./(-sim_pcoeff(1, :)), 2);
    expmu = exp(mu);
    cons_surplus = mean(log(sum(expmu, 1) + 1)./(-sim_pcoeff(1, :)), 2);
    
    if any(isnan(cons_surplus))
        save('ws_nan_in_cs.mat');
        error('stop the code');
    end
end

